<?php

/**
 * 这是用户操作模块member
 * Enter description here ...
 * @author Administrator
 *
 */
class main extends spController
{
	function __construct(){
		parent::__construct();
		$system = spClass("system");
		$sys=$system->findAll();
		$this->tpl_title = $sys[0][name];
		$this->email = $_SESSION["userinfo"]["mail"];
		if($_SESSION["userinfo"]["mail"]==""){
			$this->error('请先登录', "index.php");
		}
	}
	/**
	 * 原首页
	 * Enter description here ...
	 */
	function index(){
		$this->site();
	}
	/**
	 * 添加网站
	 */
	function addsite()
	{
		$this->contents = "添加网站";
		$user = spClass('user');
		$this->value=$user->u_status();
		//$this->ssdd = $_SESSION["userinfo"]["acl"];
		$group = spClass('group');
		$this->group=$group->s_group();
		
		if($this->spArgs(act)=="http"){
			$this->act="http";
		}else {
			$this->act="ping";
		}
		$this->display("member/main_add.html");
	}
	function insert() 
	{
		$data = $this->spArgs ();
		if($data['mclass']=='http'){
			$site = spClass ( 'site' );
		}else{
			$site = spClass ( 'psite' );
		}
		$rows = array(
			'domain' => $data ['domain'], 
			'title' => $data ['title'] 
		);
		$results = $site->spVerifier ( $rows );
		if (false == $results){
			if($site->insert ( $data )){
				$this->success('添加成功',spUrl("main","addsite"));
			}
		} else{
			foreach ( $results as $item ){
				foreach ( $item as $msg ){
					$this->error ( $msg, spUrl ( "main", "addsite" ) );
				}
			}
		}
		unset($site);
	}
	/**
	 * 概述
	 * Enter description here ...
	 */
	function site()
	{
		$site=spClass('site');
		$this->fault=$site->b_fault();
		$this->normal=$site->b_normal();
		$result=$site->a_site($this->spArgs('page', 1), 10 );
		$this->list=$result['list'];
		$this->pager=$result['pagedata'];
		unset($site);

		$notice = spClass('notice');
		$notic=$notice->find(array('noticeid'=>1));
		$this->notice = $notic['notice'];
		$user=array(
			'username'=>$_SESSION["userinfo"]["username"],
			'lasttime'=>date("Y-m-d H:i:s", $_SESSION["userinfo"]["lasttime"])
		);
		$this->user=$user;
		unset($user);
		$this->nav="class='nav_on'";
		$this->display("member/main_site.html");
	}
	/**
	 * 监控页
	 * Enter description here ...
	 */
	function detection()
	{
		$site=spClass('site');
		$result=$site->a_site($this->spArgs('page', 1), 10, $this->spArgs('group'));
		$this->list=$result['list'];
		$this->pager=$result['pagedata'];
		
		$group = spClass('group');
		$this->group=$group->s_group();
		$this->display("member/main_detection.html");
	}
	/**
	 * 编辑网站
	 * Enter description here ...
	 */
	function edit()
	{
		$site=spClass('site');		
		$result=$site->a_site($this->spArgs('page', 1), 10 );
		$this->list=$result['list'];
		$this->pager=$result['pagedata'];
		
		$psite=spClass('psite');
		$presult=$psite->a_site($this->spArgs('ppage', 1), 10 );
		$this->plist=$presult['list'];
		$this->ppager=$presult['pagedata'];
		$this->display("member/main_edit.html");
	}
	/**
	 * 更新操作
	 * Enter description here ...
	 */
	function edit_update()
	{		
		$data = $this->spArgs();
		if($data['mclass']=='http'){
		$Model = spClass('site');
		}else {
			$Model = spClass('psite');
		}
		$Model->create();
		$data['userid']=$_SESSION["userinfo"]["userid"];
		$data['groupid']='1';
        $data['lasttime']=strtotime(date("Y-m-d H:i:s"));
			$rows = array(
				'domain' => $data['domain'], 
				'title' => $data['title']
			);
			$results = $Model->spVerifier($rows);
		if (false == $results){
			$where ['siteid'] = $this->spArgs ( 'siteid' );
			if ($Model->update ( $where, $data )){
				$this->success('修改成功',spUrl("main","edit"));
				echo $where ['siteid'];
			} 
		}
		else{
			foreach($results as $item){
				foreach($item as $msg){ 
					$this->error($msg,spUrl("main","edit"));
				}
			}
		 }
	}
	/**
	 * 删除网站
	 * Enter description here ...
	 */
	function del_site()
	{
		$date=$this->spArgs();
		$condition = array(
			'siteid'=>$date['siteid']
		);
		if($date['mclass']=='http'){
			$Model = spClass('site');
			$record = spClass('record');
			$alarm = spClass('alarm');
			if($Model->delete($condition)){
				$record->delete($condition);
				$alarm->delete($condition);
				$this->success('删除成功',spUrl("main","edit"));
			}
		}else{
			$Model = spClass('psite');
			$record = spClass('ping');
			$alarm = spClass('alarm');
			if($Model->delete($condition)){
				$record->delete($condition);
				$this->success('删除成功',spUrl("main","edit"));
			}
		}
	}
	/**
	 * http检测记录
	 */
	function record()
	{
		$siteid = $this->spArgs('siteid');
		
		$conditions = "siteid = '$siteid'";		
		$record = spClass('record');
		$this->results = $record->spLinker()->spPager($this->spArgs('page', 1), 10)->findAll($conditions, 'addtime DESC');
		$this->pager = $record->spPager()->getPager();
		
		$drecord = spClass('drecord');
		$this->dresults = $drecord->spLinker()->spPager($this->spArgs('dpage', 1), 10)->findAll($conditions, 'addtime DESC');
		$this->dpager = $drecord->spPager()->getPager();
		
		$this->fault = $record->one_fault($siteid);
		$this->normal = $record->one_normal($siteid);
		$this->siteid=$siteid;
		$this->title=$this->spArgs("title");
		$this->domain = $this->spArgs("domain");
		
		$d_time = spClass('my_fun');
		$t = date("Y-m-d H:i:s",strtotime("0 month"));		
		$f = $d_time->TheMonth($t);
		$this->tmonth=$f;
		$this->nmonth=$d_time->TheMonth(date("Y-m-01",strtotime("-1 month")));
		$this->cmonth=$d_time->TheMonth(date("Y-m-01",strtotime("-2 month")));
		$this->contents = "网站检测记录详情";
		$this->display("member/main_record.html");
	}
	/**
	 * ping检测网站
	 * Enter description here ...
	 */
	function ping_site()
	{
		$site = spClass('psite');
		$result=$site->a_site( $this->spArgs('page', 1), 10, $this->spArgs('group') );
		$this->list=$result['list'];
		$this->pager=$result['pagedata'];
		
		$group = spClass('group');
		$this->group=$group->s_group();
		$this->display("member/main_ping_site.html");		
	}
	/**
	 * ping记录
	 * Enter description here ...
	 */
	function ping()
	{
		$siteid = $this->spArgs('siteid');
		
		$conditions['siteid'] = $siteid;
		$ping = spClass('ping');
		$this->results = $ping->spPager($this->spArgs('page', 1), 10)->findAll($conditions, 'p_time DESC');
		$this->pager = $ping->spPager()->getPager();		

		$this->siteid=$siteid;
		$this->title=$this->spArgs("title");
		$this->domain = $this->spArgs("domain");
		$this->contents = "网站检测记录详情";
		$this->display("member/main_ping_record.html");
	}
	/**
	 * 告警
	 * Enter description here ...
	 */
	function alarm()
	{
		$alarm = spClass('alarm');
		$siteid=$this->spArgs('siteid');
		if(isset($siteid)){
			$conditions= array(
				"user_id" => $_SESSION["userinfo"]["userid"],
				"siteid"=>$this->spArgs('siteid')
			);
		}else {
			$conditions= array(
				"user_id" => $_SESSION["userinfo"]["userid"],
			);
		}
		$this->result = $alarm->spLinker()->spPager($this->spArgs('page', 1), 20)->findAll($conditions, 'addtime DESC');
		$userid = $_SESSION["userinfo"]["userid"];
		$n=$alarm->spPager()->getPager();	
		$this->pager = $alarm->spPager()->getPager();	
		
		$site=spClass('site');
		$result=$site->a_site($this->spArgs('page', 1), 10 );
		$this->list=$result['list'];
		$this->ppager=$result['pagedata'];
		
		$this->display("member/main_alarm.html");
	}
	//告警历史
//	function alarm_history()
//	{
//		$this->date1 = date("Y-m");	
//		$alarm = spClass('alarm');
//		
//		$this->list = $alarm->n2_moth('0', $this->spArgs('page', 1), 10 );
//		$this->pager = $alarm->pagedata;
//		
//		$this->display("member/main_alarm_history.html");
//	}
//	function limit_alarm()
//	{
//		$data = $this->spArgs();
//		$b = strtotime($data['btime'].'oo:oo:oo');
//		$a = strtotime($data['atime'].'23:59:59');
//		$alarm = spClass('alarm');
//		$this->list = $alarm->nmoth($b,$a, $this->spArgs('page', 1), 10 );
//		$this->pager = $alarm->pagedata;
//		$this->display("member/main_nalarm.html");
//	}
//	function statistics()
//	{
//		$site = spClass('site');
//		$conditions=array('userid'=>$_SESSION['userinfo']['userid']);
//		$this->list=$site->findAll($conditions);
//		$this->display("member/main_statistics.html");
//	}
	/**
	 * 分类管理
	 * Enter description here ...
	 */
	function v_group()
	{
		$group = spClass('group');
		$data=$this->spArgs();
		if($data['name']!="" && $data['groupid']==""){
			$data['addtime']=strtotime(date("Y-m-d H:i:s"));
			if($group->create($data)){
				$this->success('添加成功', spUrl("main","v_group"));
			}else{
				$this->error('添加失败', spUrl("main","v_group"));
			}
		}elseif (!isset($data['act'])){
			$this->one_group=$group->find(array('groupid'=>$data["groupid"]));
		}elseif ($data['name']!="" && $data['groupid']!=""){
			$data['addtime']=strtotime(date("Y-m-d H:i:s"));
			$conditions['groupid']=$data['groupid'];
			$row['name']=$data['name'];
			if($group->update($conditions,$row)){
				$this->success('修改成功', spUrl("main","v_group"));
			}else{
				$this->error('修改失败', spUrl("main","v_group"));
			}
		}elseif ($data['act']=="del"){
			$conditions['groupid']=$data['groupid'];
			if($group->delete($conditions)){
				$this->success('删除成功', spUrl("main","v_group"));
			}else{
				$this->error('删除失败', spUrl("main","v_group"));
			}
		}
		
		$this->group=$group->s_group();
		$this->display("member/main_group.html");

	}
	/**
	 * 用户账户管理
	 * Enter description here ...
	 */
	function u_psd()
	{
		$user=spClass('user');
		$conditions['userid'] = $_SESSION["userinfo"]["userid"];
		$list = $user->findall($conditions);

		$this->list=$list;
		$this->display("member/user_edit.html");		
	}
	function user_edit()
	{
		$data = $this->spArgs();
		$user = spClass('user');
		$where = array(
			userid => $data['userid']
		);
		$data['password'] =md5($this->spArgs("password")); 
		if($user->update ( $where, $data )){
			$this->success('修改成功', spUrl("main","u_psd"));
		}else{
			$this->error('修改失败', spUrl("main","u_psd"));
		}
	}
	/**
	 * 密码修改
	 * Enter description here ...
	 */
	function password()
	{
		$data = $this->spArgs();
		if($da = $data['password']){
			if($data['password']==$data['npassword']){
				$user = spClass('user');
				$where = array(
				userid => $_SESSION['userinfo']['userid'],
				);
				$dt = array('password' =>md5($this->spArgs("password")));
				if($user->update ( $where, $dt )){
					$this->success('修改成功', spUrl("main","password"));
				}else{
					$this->error('修改失败', spUrl("main","password"));
				}
			}else{
				$this->error('两次密码输入不一致，请重新输入', spUrl("main", "password"));
			}
		}
		$user=spClass('user');
		$conditions= array(
			"userid" => $_SESSION["userinfo"]["userid"],
		);
		$list = $user->findall($conditions);
		$this->list=$list;
		$this->display("member/main_password.html");
	}
	/**
	 * EMAIL修改
	 * Enter description here ...
	 */
	function email()
	{
		$data = $this->spArgs();
		if($da = $data['email']){
			if($data['email']==$data['nemail']){
				$user = spClass('user');
				$where = array(
					userid => $_SESSION['userinfo']['userid'],
				);
				$dt = array('mail' =>$this->spArgs("email"));
				if($user->update ( $where, $dt )){
					$this->success('修改成功', spUrl("main","email"));
				}else{
					$this->error('修改失败', spUrl("main","email"));
				}
			}else{
				$this->error('两次密码输入不一致，请重新输入', spUrl("main", "email"));
			}
		}
		$user=spClass('user');
		$conditions= array(
			"userid" => $_SESSION["userinfo"]["userid"],
		);
		$list = $user->findall($conditions);

		$this->list=$list;
		$this->display("member/main_email.html");
	}
	/**
	 * 用户反馈
	 */
	function feedback()
	{
		if($back=$this->spArgs("feedback"))	{
			$feedback = spClass('feedback');
			$data = array(
				'feedbacks'=>$this->spArgs("feedback"),
				'userid'=>$_SESSION['userinfo']['userid'],
			);
			$feedback->create($data);
		}
		$this->display("member/main_feedback.html");	
	}

}	